package com.entities.projecttask.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.managers.SessionManager;
import com.core.protocols.IServiceLayerDelegate;
import com.core.servlets.TaskTrackerServlet;
import com.core.utils.GlobalConstants;
import com.entities.projecttask.servicelayer.ProjectTasksListLayer;

/**
 * Servlet implementation class ProjectDelete
 */
public class ProjectTaskDelete
        extends TaskTrackerServlet
        implements IServiceLayerDelegate {

	private String              redirection  = null;
	private HttpServletResponse httpResponse = null;



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		if ( httpResponse == null ) {
			return;
		}

		if ( exception != null ) {

		}

		if ( redirection == null ) {
			return;
		}

		try {
			httpResponse.sendRedirect(redirection);
		} catch ( IOException e ) {
			e.printStackTrace();
		}

	}

	private static final long serialVersionUID = 1L;



	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProjectTaskDelete() {

		super();

	}



	@Override
	protected void doPost ( HttpServletRequest request,
	                        HttpServletResponse response )
	        throws ServletException, IOException {

		this.httpResponse = response;

		String taskId = request.getParameter(GlobalConstants.SERVLET_PARAM_KEY);
		Integer objectId = null;
		try{
			objectId = Integer.parseInt(taskId);
		}catch(NumberFormatException e){
			objectId = null;
		}

		ProjectTasksListLayer projectTasksListLayer = new ProjectTasksListLayer();
		projectTasksListLayer.setLayerDelegate(this);
		projectTasksListLayer.deleteObject(objectId);

	}



	@Override
	protected void doGet ( HttpServletRequest request,
	                       HttpServletResponse response )
	        throws ServletException, IOException {


		redirection = SessionManager.sharedInstance().lastExecutedURL();
		super.doGet(request, response);
	}

}
